home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / macro.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-06  |  4.3 KB  |  222 lines

  1. /* cfengine for GNU
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained 
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  
  24. */
  25.  
  26.  
  27. #include "cf.defs.h"
  28. #include "cf.extern.h"
  29.  
  30.  
  31. char *HASH[hashtablesize];
  32.  
  33.  
  34. /*******************************************************************/
  35. /* Macro substitution based on HASH-table                          */
  36. /*******************************************************************/
  37.  
  38. InitHashTable()
  39.  
  40. { int i;
  41.  
  42. for (i = 0; i < hashtablesize; i++)
  43.    {
  44.    HASH[i] = NULL;
  45.    }
  46. }
  47.  
  48.  
  49. /*******************************************************************/
  50.  
  51. Hash(name)
  52.  
  53. char *name;
  54.  
  55. { int i, slot = 0;
  56.  
  57.  for (i = 0; name[i] != '\0'; i++)
  58.    {
  59.    slot = (macroalphabet * slot + name[i]) % hashtablesize;
  60.    }
  61.  
  62. return slot;
  63. }
  64.  
  65. /*******************************************************************/
  66.  
  67. AddMacroValue(name,value)                           /* Like putenv */
  68.  
  69. char *name,*value;
  70.  
  71. { char *sp, buffer[bufsize];
  72.   int slot;
  73.  
  74. Debug1("AddMacroValue(%s=%s)\n",name,value);
  75.  
  76. if (name == NULL || value == NULL)
  77.    {
  78.    yyerror("Bad macro");
  79.    }
  80.  
  81. if (strlen(name) > maxvarsize)
  82.    {
  83.    yyerror("macro name too long");
  84.    return;
  85.    }
  86.  
  87. buffer[0] = '\0';
  88.  
  89. sprintf(buffer,"%s=%s",name,value);
  90.  
  91. if ((sp = malloc(strlen(buffer)+1)) == NULL)
  92.    {
  93.    perror("malloc");
  94.    FatalError("aborting");
  95.    }
  96.  
  97. strcpy(sp,buffer);
  98.  
  99. slot = Hash(name);
  100.  
  101. if (HASH[slot] != 0)
  102.    {
  103.    if (CompareMacro(name,HASH[slot]) == 0)
  104.       {
  105.       sprintf(VBUFF,"Redefinition of macro %s=%s",name,value);
  106.       Warning(VBUFF);
  107.       free(HASH[slot]);
  108.       HASH[slot] = sp;
  109.       return;
  110.       }
  111.  
  112.    while ((HASH[++slot % hashtablesize] != 0))
  113.       {
  114.       if (slot == hashtablesize-1)
  115.          {
  116.          slot = 0;
  117.          }
  118.       if (slot == Hash(name))
  119.          {
  120.          FatalError("AddMacroValue - internal error #1");
  121.          }
  122.       }
  123.    }
  124.  
  125. HASH[slot] = sp;
  126.  
  127. Debug1("Added Macro at hash address %d: %s\n",slot,sp);
  128. }
  129.  
  130. /*******************************************************************/
  131.  
  132. char *GetMacroValue(name)
  133.  
  134. char *name;
  135.  
  136. { char *sp;
  137.   int slot,i;
  138.  
  139. i = slot = Hash(name);
  140.  
  141. if (CompareMacro(name,HASH[slot]) != 0)
  142.    {
  143.    while (true)
  144.       {
  145.       i++;
  146.  
  147.       if (i >= hashtablesize-1)
  148.          {
  149.          i = 0;
  150.          }
  151.  
  152.       if (CompareMacro(name,HASH[i]) == 0)
  153.          {
  154.          for(sp = HASH[i]; *sp != '='; sp++)
  155.             {
  156.             }
  157.  
  158.          return(sp+1);
  159.          }
  160.  
  161.       if (i == slot-1)
  162.          {
  163.          return(getenv(name));  /* Look in environment if not found */
  164.          }
  165.       }
  166.    }
  167. else
  168.    {
  169.    for(sp = HASH[slot]; *sp != '='; sp++)
  170.       {
  171.       }
  172.  
  173.    return(sp+1);
  174.    }   
  175. }
  176.  
  177. /*******************************************************************/
  178.  
  179. RecordMacroId(name)
  180.  
  181. char *name;
  182.  
  183. {
  184. Debug("RecordMacroId(%s)\n",name);
  185. strcpy(CURRENTITEM,name);
  186. }
  187.  
  188. /*******************************************************************/
  189.  
  190. CompareMacro(name,macro)
  191.  
  192. char *name,*macro;
  193.  
  194. { char buffer[bufsize];
  195.  
  196. if (macro == NULL || name == NULL)
  197.    {
  198.    return 1;
  199.    }
  200.  
  201. sscanf(macro,"%[^=]",buffer);
  202.  
  203. Debug1("CompareMacro(%s,%s)=%s\n",name,macro,buffer);
  204. return(strcmp(buffer,name));
  205. }
  206.  
  207. /*******************************************************************/
  208.  
  209. DeleteMacros()
  210.  
  211. { int i;
  212.  
  213. for (i = 0; i < hashtablesize; i++)
  214.    {
  215.    if (HASH[i] != NULL)
  216.       {
  217.       free(HASH[i]);
  218.       HASH[i] = NULL;
  219.       }
  220.    }
  221. }
  222.